#include "DataBaseAgent.h"
#include <QMessageBox>

DataBaseAgent::DataBaseAgent()
{
	if (!QSqlDatabase::isDriverAvailable ("QSQLITE"))
	{
		QMessageBox::critical(0,"dbConnectErrorStr",QSqlDatabase::drivers().join(" "));
		return;
	}
	_sdb = QSqlDatabase::addDatabase("QSQLITE");

	_crypt = NULL;
}

void DataBaseAgent::setCryptoAgent(CryptoAgent* agent)
{
	_crypt = agent;
}

QString DataBaseAgent::getCurrentFile()
{
	return _currentDBFile;
}

bool DataBaseAgent::setDataBase(QString filename)
{
	_sdb.setDatabaseName(filename);

	_sdb.setUserName("MisakaMikoto");
	_sdb.setPassword("^_^nya^_^");

	if (!_sdb.open()) {
		return false;
	}

	QSqlQuery query;
	bool b = query.exec("CREATE TABLE IF NOT EXISTS Notes (id INTEGER PRIMARY KEY AUTOINCREMENT, url TEXT, login TEXT, pass TEXT, comment TEXT);");
	if (!b)
	{
		QMessageBox::critical(0,"Create table error",_sdb.lastError().text());
	}

	_currentDBFile = filename;

	return true;
}

void DataBaseAgent::close()
{
	_sdb.close();
}

QList<Note*> DataBaseAgent::getNotesList()
{
	QList<Note*> noteList;
	if(_crypt == NULL){
		return noteList;
	}

	QSqlQuery query;
	bool b = query.exec("SELECT * FROM Notes;");
	if (!b)
	{
		QMessageBox::critical(0,"Select from table",_sdb.lastError().text());
	}

	while (query.next()) {
		Note* note = new Note();
		note->setId(query.value(0).toInt());
		note->setUrl(query.value(1).toString());
		note->setLogin(query.value(2).toString());
		note->setPass(query.value(3).toString());
		note->setComment(query.value(4).toString());

		_crypt->decryptNote(note);
		noteList.append(note);
	}
	return noteList;
}

int DataBaseAgent::addNote(Note note)
{
	if(_crypt == NULL) return -1;

	_crypt->encryptNote(&note);

	QSqlQuery query;
	QString str = QString("INSERT INTO Notes(url, login, pass, comment) VALUES ('%1', '%2', '%3', '%4');")
			.arg(note.getUrl())
			.arg(note.getLogin())
			.arg(note.getPass())
			.arg(note.getComment());

	bool b = query.exec(str);
	if (!b)
	{
		QMessageBox::critical(0,"Add note",_sdb.lastError().text());
		return -1;
	}

	b = query.exec("SELECT MAX(id) FROM Notes;");
	if (!b)
	{
		QMessageBox::critical(0,"Add note",_sdb.lastError().text());
		return -1;
	}

	query.next();
	int num = query.value(0).toInt();
	return num;
}

bool DataBaseAgent::removeNote(Note note)
{
	if(_crypt == NULL) return false;

	QSqlQuery query;
	QString str = QString("DELETE FROM Notes WHERE id=%1;").arg(note.getId());

	bool b = query.exec(str);
	if (!b)
	{
		QMessageBox::critical(0,"Delete note",_sdb.lastError().text());
		return false;
	}

	_sdb.commit();
	return true;
}

bool DataBaseAgent::updateNote(Note note)
{
	if(_crypt == NULL) return false;

	_crypt->encryptNote(&note);

	QSqlQuery query;
	QString str = QString("UPDATE Notes SET url='%2', login='%3', pass='%4', comment='%5' WHERE id=%1;")
			.arg(note.getId())
			.arg(note.getUrl())
			.arg(note.getLogin())
			.arg(note.getPass())
			.arg(note.getComment());

	bool b = query.exec(str);
	if (!b)
	{
		QMessageBox::critical(0,"Update note",_sdb.lastError().text());
		return false;
	}

	_sdb.commit();
	return true;
}

bool DataBaseAgent::clearAll()
{
	QSqlQuery query;
	bool b = query.exec("DELETE FROM Notes;");
	if (!b)
	{
		QMessageBox::critical(0,"Delete all notes",_sdb.lastError().text());
		return false;
	}

	_sdb.commit();
	return true;
}
